# -- coding: utf-8 --
from selenium.webdriver.support.select import Select


class UITools:

    driver = None

    def get_driver(self, browser):
        from selenium import webdriver
        driver = None
        if hasattr(webdriver, browser):
            self.driver = getattr(webdriver, browser)()
        else:
            self.driver = webdriver.Firefox()
        return self.driver

    def get_element(self, locator):  # locator的格式是{"by":"XPATH", "what":"opton[1]"}
        by = locator['by']
        what = locator['what']
        driver = self.get_driver('Firefox')
        from selenium.webdriver.common.by import By
        try:
            return driver.find_element(getattr(By, by), what)   # 找到了元素
        except Exception as e:
            return None

    def select_option(self, element):
        size = len(Select(element).options)
        import random
        random_index = random.randint(0, size-1)
        Select(element).select_by_index(random_index)


class DBTools:
    def update(self, connn_info, sql):
        import pymysql
        conn = pymysql.connect(**connn_info)
        cur = conn.cursor()
        cur.execute(sql)
        conn.commit()
        cur.close()
        conn.close()


if __name__ == '__main__':

    ui = UITools()
    ui.get_driver('Firefox')